package cn.jxwazx.sheinspringboot.holder;


import cn.jxwazx.sheinspringboot.entity.po.UserInfo;
import cn.jxwazx.sheinspringboot.exception.MyException;

/**
 * @author jie
 */
public class UserHolder {
    private static ThreadLocal<UserInfo> LOCAL = new ThreadLocal<UserInfo>();

    private UserHolder() {
    }

    public static UserInfo getLocal() {
        UserInfo loginUserInfo = LOCAL.get();
        if(loginUserInfo == null)
        {
            throw new MyException("LoginUserInfo非法访问，请先登入");
        }
        return loginUserInfo;
    }

    public static Long getId() {
        return LOCAL.get() == null ?  null : LOCAL.get().getId();
    }


    public static void setLocal(UserInfo local) {
        UserHolder.LOCAL.set(local);
    }

    public static void remove() {
        LOCAL.remove();
    }

    public static String getToken() {
        return LOCAL.get() == null ?  null : LOCAL.get().getToken();

    }
}
